﻿Imports System.IO

Module SearchFormModule

    Function enumImages() As List(Of String)
        Dim filescontainer As New System.IO.DirectoryInfo(MainPath)
        Dim possibleExtensions As New List(Of String) From {".jpg", ".jpeg", ".bmp", ".gif", ".png"}
        Dim retval As New List(Of String)
        For Each File In filescontainer.GetFiles
            If possibleExtensions.Contains(File.Extension) Then
                retval.Add(File.FullName)
            End If
        Next
        Return retval
    End Function



    Function enumImages(ByVal Datum As String) As List(Of ImageEnumType)
        Dim filescontainer As New System.IO.DirectoryInfo(MainPath)
        Dim possibleExtensions As New List(Of String) From {".jpg", ".jpeg", ".bmp", ".gif", ".png"}
        Dim retval As New List(Of ImageEnumType)
        For Each File In filescontainer.GetFiles

            If possibleExtensions.Contains(File.Extension) Then
                Dim nn = Split(File.Name, "-")(0)
                Dim no = Replace(Split(File.Name, "-")(1), ".", ":")
                If nn = Datum Then
                    Dim ntype As New ImageEnumType
                    With ntype
                        .Name = File.Name
                        .Source = File.Name
                        .Uhrzeit = no
                    End With
                    retval.Add(ntype)
                End If
            End If
        Next
        Return retval
    End Function

    Function FullenumMitschrift() As List(Of ImageMitschriftType)
        Dim filescontainer As New System.IO.DirectoryInfo(MainPath)
        Dim possibleExtensions As New List(Of String) From {".jpg", ".jpeg", ".bmp", ".gif", ".png"}
        Dim retval As New List(Of ImageMitschriftType)
        For Each File In filescontainer.GetFiles
            If File.Name.StartsWith("R") Then
                If possibleExtensions.Contains(File.Extension) Then
                    Dim splits = Split(File.Name, "---")
                    Dim rayons = Split(splits(0), "-")(0)
                    Dim datums = Split(splits(0), "-")(1)

                    Dim PGSTVOStrings = splits(1).Trim("-")

                    Dim pgstvosplit = Split(PGSTVOStrings, "--")

                    Dim pgstr As String = ""
                    Dim stvostr As String = ""

                    For Each a In pgstvosplit
                        If a.StartsWith("P") Or a.StartsWith("p") Then
                            pgstr &= Replace(a, "Pg", "").Trim & ","
                        End If
                        If a.StartsWith("S") Or a.StartsWith("s") Then
                            stvostr &= Replace(a, "StVo", "").Trim & ","
                        End If
                    Next

                    Dim ntype As New ImageMitschriftType
                    With ntype
                        .Datum = datums
                        .Name = File.Name
                        .PgStrings = pgstr
                        .StVOStrings = stvostr
                        .Rayon = rayons
                        .Source = File.Name
                    End With
                    retval.Add(ntype)
                End If
            End If
        Next
        Return retval
    End Function


    Function SearchMitschrift(ByVal Rayon As String, ByVal Datum As String, ByVal Pg As String, ByVal Stvo As String)
        Dim listof = FullenumMitschrift()
        Debug.WriteLine("listof: " & listof.Count)

        Dim query As IEnumerable(Of ImageMitschriftType) = listof


        If Rayon <> "" Then
            query = query.Where(Function(x)
                                    If x.Rayon = Rayon Then
                                        Return True
                                    Else
                                        Return False
                                    End If
                                End Function)
        End If

        If Datum <> "" Then
            query = query.Where(Function(x)
                                    If x.Datum = Datum Then
                                        Return True
                                    Else
                                        Return False
                                    End If
                                End Function)
        End If

        If Pg <> "" Then
            query = query.Where(Function(x)
                                    If x.PgStrings.Contains(Pg) Then
                                        Return True
                                    Else
                                        Return False
                                    End If
                                End Function)
        End If

        If Stvo <> "" Then
            query = query.Where(Function(x)
                                    If x.StVOStrings.Contains(Stvo) Then
                                        Return True
                                    Else
                                        Return False
                                    End If
                                End Function)
        End If

        Dim retval As New List(Of ImageMitschriftType)
        For Each a In query
            retval.Add(a)
        Next

        Return retval
    End Function
End Module

Public Class ImageEnumType
    Public Property Name As String
    Public Property Source As String
    Public Property Uhrzeit As String
End Class

Public Class ImageMitschriftType
    Public Property Datum As String
    Public Property Rayon As String
    Public Property PgStrings As String
    Public Property StVOStrings As String
    Public Property Name As String
    Public Property Source As String
End Class